<?php

namespace App\Repositories\Eloquent\Exm;

use Illuminate\Container\Container as App;
use App\Repositories\Eloquent\Repository;
use DB;

class QuestionRepository extends Repository
{

    /**
     * @param App
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Exm\Question';
    }

    /**
     * 按题型过滤
     *
     * @return \Illuminate\Http\Response
     */
    public function index($type = '')
    {
        $result = $this->model
                ->with('options')
                ->orderBy('id', 'asc');

        return $result;
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store($request)
    {
        /*$data = array();
        for($i=0;$i<=3;$i++){
            $data[] = ['content'=>$request->content[$i],'correct'=>$request->correct[$i]];
        }
        dd($data);*/
        $data = array();

        DB::beginTransaction();

        try{
            $question = $this->create([
                'title'      => $request->title,
                'type'      => is_null($request->type) ? 0:$request->type,
            ]);
            if (!!$question) {
                for($i=0;$i<=3;$i++){
                    $data[] = [
                        'question_id'=>$question->id,
                        'content'=>$request->content[$i],
                        'correct'=>$request->correct[$i]
                    ];
                }
                DB::table('exm_options')->insert($data);
            }

            DB::commit();

            return $this->responseSuccess('增加题目成功', $question, route('question.index'));

        } catch (Exception $e) {

            DB::rollback();

            return $this->responseFail(500,'增加题目失败');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $question = $this->find($id);

        if (!$question)
            return $this->responseFail(404, '题目信息不正确');

        return $question;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function renew($request, $id)
    {
        $data = array();

        DB::beginTransaction();

        try{
            $question = $this->update([
                'title'      => $request->title,
                'type'      => is_null($request->type) ? 0:$request->type,
            ], $id);

            for($i=0;$i<=3;$i++){
                $data[] = [
                    'id'=>$request->id[$i],
                    'content'=>$request->content[$i],
                    'correct'=>$request->correct[$i]
                ];
            }
            $this->updateBatch('bank_exm_options', $data);

            DB::commit();

            return $this->responseSuccess('更新题目成功', $question, route('question.index'));

        } catch (Exception $e) {

            DB::rollback();

            return $this->responseFail(500,'更新题目失败');
        }
    }



    public function updateBatch($tableName = "", $multipleData = array()){

        if( $tableName && !empty($multipleData) ) {

            // column or fields to update
            $updateColumn = array_keys($multipleData[0]);
            $referenceColumn = $updateColumn[0]; //e.g id
            unset($updateColumn[0]);
            $whereIn = "";

            $q = "UPDATE ".$tableName." SET ";
            foreach ( $updateColumn as $uColumn ) {
                $q .=  $uColumn." = CASE ";

                foreach( $multipleData as $data ) {
                    $q .= "WHEN ".$referenceColumn." = ".$data[$referenceColumn]." THEN '".$data[$uColumn]."' ";
                }
                $q .= "ELSE ".$uColumn." END, ";
            }
            foreach( $multipleData as $data ) {
                $whereIn .= "'".$data[$referenceColumn]."', ";
            }
            $q = rtrim($q, ", ")." WHERE ".$referenceColumn." IN (".  rtrim($whereIn, ', ').")";

            // Update
            return DB::update(DB::raw($q));

        } else {
            return false;
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


}